package StudyCode.summary.mycollection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ReturnsStrippedValueFromTheSpecifiedArrayOfStuff {
	public static Object[] replaceAll(final Object[] objs, final String str) {
		if (isEmpty(objs, false)) {
			return null;
		}
		List<Object> list = new ArrayList<Object>(
				Arrays.asList(trimArray(objs)));
		list.removeAll(Collections.singletonList(str));
		return list.toArray(new Object[list.size()]);
	}

	public static boolean isEmpty(final Object[] objs,
	                              Boolean isContentsCheck) {
		if (objs == null || objs.length < 1) {
			return true;
		}
		if (isContentsCheck) {
			for (Object obj : objs) {
				if (isEmpty(obj)) {
					return true;
				}
			}
		}
		return false;
	}

	public static boolean isEmpty(final Object obj) {
		if (obj == null) {
			return true;
		}
		if (obj instanceof String) {
			return isEmptyStr((String) obj);
		}
		return false;
	}


	private static Object[] trimArray(Object[] objs) {
		if (isEmpty(objs)) {
			return null;
		}
		int i = 0;
		Object[] cpObjs = new Object[objs.length];
		for (Object obj : objs) {
			try {
				cpObjs[i] = obj.toString().trim();
			} catch (Exception e) {
				cpObjs[i] = obj;
			} finally {
				i++;
			}
		}
		return cpObjs;
	}


	private static boolean isEmptyStr(final String str) {
		return str == null || str.length() < 1;
	}
}
